package com.xm.xmapp.activity;

import android.content.Intent;
import android.graphics.Point;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Gravity;

import com.alibaba.fastjson.JSON;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.navi.model.NaviLatLng;
import com.xm.xmapp.R;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.fragment.MainBottomFragment;
import com.xm.xmapp.fragment.MainTopFragment;
import com.xm.xmapp.http.NetWorkData;
import com.xm.xmapp.map.ClusterClickListener;
import com.xm.xmapp.map.ClusterItem;
import com.xm.xmapp.map.MapOverlay;
import com.xm.xmapp.myinterface.MainInterFaceAble;
import com.xm.xmapp.req.ReqAllNetWorkCoordsBean;
import com.xm.xmapp.resp.RespAllNetWorkCoordsBean;
import com.xm.xmapp.tool.AndroidTool;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by LDW on 2017/7/17.
 */
public class MainActivity extends BaseActivity implements AMap.OnMyLocationChangeListener, ClusterClickListener {

    private static final int LOGIN = 1;
    private static final int All_NET_WORK_COORDS = 2;
    @Bind(R.id.mMapView)
    MapView mMapView;
    @Bind(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    private AMap aMap;
    private MapOverlay mMapOverlay;
    private MainTopFragment mMainTopFragment;
    private MainBottomFragment mMainBottomFragment;
    private MainInterFaceAble mMainInterFaceAble;
    private Location location;
    private int mStatus;
    private ArrayList<ClusterItem> mPileList = new ArrayList<ClusterItem>();// 充电桩网点显示
    private ArrayList<ClusterItem> mShortList = new ArrayList<ClusterItem>();// 分时租赁网点
    private ArrayList<ClusterItem> mLongList = new ArrayList<ClusterItem>();// 长租网点

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @Override
    public void onMyLocationChange(Location location) {
        if (location.getLatitude() != 0) {
            this.location = location;
        }
    }

    public void setmStatus(int mStatus) {
        this.mStatus = mStatus;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图
        mMapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
        startLoction();
        mMapOverlay = new MapOverlay(aMap);
        mMapOverlay.setOnClusterClickListener(this);
        mMainInterFaceAble = new MainInterFaceAble() {
            @Override
            public Object uIChange(int type, Object data) {
                switch (type) {
                    case NAVIGATION:
                        setmStatus((int)data);
                        Intent intent = new Intent(MyAppcation.getMyAppcation(), BasicNaviActivity.class);
                        NaviLatLng mEndLatlng = new NaviLatLng(39.925846, 116.432765);
                        NaviLatLng mStartLatlng = new NaviLatLng(39.925041, 116.437901);
                        intent.putExtra("mStartLatlng", mStartLatlng);
                        intent.putExtra("mEndLatlng", mEndLatlng);
                        startActivity(intent);
                        break;
                    case GET_LOCTION:
                        startLoction();
                        break;
                    case SHOW_LEFT:
                        drawerLayout.openDrawer(Gravity.LEFT);
                        break;
                }

                return null;
            }
        };
        mMainTopFragment = (MainTopFragment) getSupportFragmentManager().findFragmentById(R.id.top_drawer);
        mMainBottomFragment = (MainBottomFragment) getSupportFragmentManager().findFragmentById(R.id.bottom_drawer);
        mMainTopFragment.setmMainInterFaceAble(mMainInterFaceAble);
        mMainBottomFragment.setmMainInterFaceAble(mMainInterFaceAble);
        initNetWorkData();
        ReqAllNetWorkCoordsBean mReqAllNetWorkCoordsBean = new ReqAllNetWorkCoordsBean();
        mReqAllNetWorkCoordsBean.times = 1;
        getNetWorkData(mReqAllNetWorkCoordsBean, All_NET_WORK_COORDS);
        setmStatus(0);
    }

    /**
     * 开始定位
     */
    private void startLoction() {
        if (location == null) {
            MyLocationStyle myLocationStyle;
            myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）如果不设置myLocationType，默认也会执行此种模式。
            myLocationStyle.interval(60 * 1000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
            myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.mipmap.home_icon_location));
            myLocationStyle.strokeColor(0x00ffffff);
            myLocationStyle.radiusFillColor(0x00ffffff);
            aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
            UiSettings mUiSettings = aMap.getUiSettings();// 拿到地图工具类
            mUiSettings.setMyLocationButtonEnabled(false);//设置默认定位按钮是否显示，非必需设置。
            mUiSettings.setMyLocationButtonEnabled(false);// 设置默认定位按钮是否显示
            mUiSettings.setTiltGesturesEnabled(false);// 禁用倾斜手势。
            mUiSettings.setRotateGesturesEnabled(false);// 禁用旋转手势。
            mUiSettings.setZoomControlsEnabled(false);//隐藏加减号
            aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
            aMap.setOnMyLocationChangeListener(this);// 设置定位监听
        } else {
            LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
            aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));
            aMap.moveCamera(CameraUpdateFactory.zoomTo(14));
        }
    }

    private void initNetWorkData() {
        mNetWorkData = new NetWorkData() {

            @Override
            public void dissMissDialog(int code) {

            }

            @Override
            public void onResponse(int code, String response) {
                Log.d("response", response);
                switch (code) {
                    case LOGIN:
                        break;
                    case All_NET_WORK_COORDS: {
                        RespAllNetWorkCoordsBean baseBean = JSON.parseObject(response, RespAllNetWorkCoordsBean.class);
                        for (RespAllNetWorkCoordsBean.PointsBean bean : baseBean.getPoints()) {
                            switch (bean.getNetwork_rent_type()) {
                                case RespAllNetWorkCoordsBean.PointsBean.TYPE_OLD:
                                    if (bean.getHandle_long_rent() == 1) {
                                        mLongList.add(bean);
                                    }
                                    mShortList.add(bean);
                                    break;
                                case RespAllNetWorkCoordsBean.PointsBean.TYPE_SHORT:
                                    mShortList.add(bean);
                                    break;
                                case RespAllNetWorkCoordsBean.PointsBean.TYPE_LONG:
                                    mLongList.add(bean);
                                    break;
                                case RespAllNetWorkCoordsBean.PointsBean.TYPE_SHORT_LONG:
                                    mLongList.add(bean);
                                    mShortList.add(bean);
                                    break;
                                default:
                                    if (bean.getHandle_long_rent() == 1) {
                                        mLongList.add(bean);
                                    }
                                    mShortList.add(bean);
                                    break;
                            }
                        }

                        switch (mStatus) {
                            case 0:
                                setNetWorkOnMap(mShortList);
                                break;
                            case 1:
                                setNetWorkOnMap(mLongList);
                                break;
                            case 2:
                                setNetWorkOnMap(mPileList);
                                break;
                            case 3:
                                setNetWorkOnMap(mShortList);

                                break;
                        }

                        break;
                    }
                    default:
                        break;
                }
            }

            @Override
            public void onErrorResponse(int code, String errorResponse) {

            }
        };
    }

    private void setNetWorkOnMap(ArrayList<ClusterItem> items) {
        mMapOverlay.setMarkerListall(items);
    }

    @Override
    protected void onDestroy() {
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy();
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }


    @Override
    public void onMarkerClick(ClusterItem cluster) {

        AndroidTool.showMessage(cluster.getNetWorkName() + "");
        switch (mStatus) {
            case 0:

                break;
            case 1:

                break;
            case 2:

                break;
            case 3:

                break;
        }
    }

    @Override
    public void onMapClick() {

    }

    @Override
    public void onCarMarkerClick(ClusterItem cluster, int item) {

    }

    @Override
    public void onClickGo(ClusterItem cluster) {

    }

    @Override
    public void onClickVisible(ClusterItem cluster) {

    }
}
